home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / PCL4W10.ZIP / CONFIG.C < prev    next >
Text File  |  1994-02-05  |  5KB  |  205 lines

  1. /*** config.c ***/
  2.  
  3. #include <stdio.h>
  4. #include "windows.h"
  5. #include "config.h"
  6. #include "expect.h"
  7. #include "sioerror.h"
  8. #include "pcl4w.h"
  9. #include "simple.h"
  10.  
  11. extern int  OnLineFlag;
  12. extern HWND hMainWnd;
  13.  
  14. static struct
  15.  {int Port;
  16.   int BaudRate;
  17.   int WordLength;
  18.   int Parity;
  19.   int StopBits;
  20.  } Config = {COM1,Baud9600,WordLength8,NoParity,OneStopBit};
  21.  
  22. static long BaudRateList[10]
  23.            = {300L,600L,1200L,2400L,4800L,9600L,19200L,38400L,57600L,115200L};
  24. static char StopBitList[2] = {'1','2'};
  25. static char ParityList[4]  = {'N','O','?','E'};
  26. static char WordLengthList[4] = {0,0,'7','8'};
  27.  
  28. static int PortMsgID[4] = {MSG_COM1,MSG_COM2,MSG_COM3,MSG_COM4};
  29. static int BaudRateMsgID[10] = {0,0,MSG_1200,MSG_2400,
  30.                                 MSG_4800,MSG_9600,MSG_19200,MSG_38400,
  31.                                 MSG_57600,MSG_115200};
  32. static int StopBitsMsgID[2] = {MSG_1_SB,MSG_2_SB};
  33. static int ParityMsgID[4] = {MSG_NONE,MSG_ODD,0,MSG_EVEN};
  34. static int WordLengthMsgID[4] = {0,0,MSG_7_DB,MSG_8_DB};
  35.  
  36. static char *LineText[2] = {"OFF", "ON"};
  37.  
  38. static void SetPSL(int,int,int);
  39. static void CheckTheMenu(int);
  40. static void UncheckTheMenu(int);
  41. static void CheckAll(void);
  42. static void UncheckAll(void);
  43. static int  UpdateTheChecks(int,int);
  44.  
  45. static LastPortID = MSG_COM1;
  46. static LastParityID = MSG_NONE;
  47. static LastStopBitsID = MSG_1_SB;
  48. static LastWordLengthID = MSG_8_DB;
  49. static LastBaudRateID = MSG_9600;
  50.  
  51. void InitConfig()
  52. {CheckAll();
  53.  SetTitle();
  54. }
  55.  
  56. void LoadConfig()
  57. {int hFile;
  58.  hFile = _lopen("simple.cfg",OF_READ | OF_SHARE_DENY_WRITE);
  59.  if(hFile<0) ErrorMessage("SIMPLE.CFG not found");
  60.  else
  61.    {/* uncheck menu selections */
  62.     UncheckAll();
  63.     /* read in new configuration */
  64.     _lread(hFile,&Config,sizeof(Config));
  65.     _lclose(hFile);
  66.     SetTitle();
  67.     /* set current menu selections */
  68.     LastPortID = PortMsgID[Config.Port];
  69.     LastParityID = ParityMsgID[Config.Parity];
  70.     LastStopBitsID = StopBitsMsgID[Config.StopBits];
  71.     LastWordLengthID = WordLengthMsgID[Config.WordLength];
  72.     LastBaudRateID = BaudRateMsgID[Config.BaudRate];
  73.     /* check current menu selections */
  74.     CheckAll();
  75.    }
  76. } /* end LoadConfig */
  77.  
  78. void SaveConfig()
  79. {int hFile;
  80.  hFile = _lcreat("simple.cfg",0);
  81.  _lwrite(hFile,&Config,sizeof(Config));
  82.  _lclose(hFile);
  83. } /* end LoadConfig */
  84.  
  85. void SetParity(int Parity)
  86. {
  87.  SetPSL(Parity,Config.StopBits,Config.WordLength);
  88.  LastParityID = UpdateTheChecks(LastParityID,ParityMsgID[Parity]);
  89. }
  90.  
  91. void SetStopBits(int StopBits)
  92. {
  93.  SetPSL(Config.Parity,StopBits,Config.WordLength);
  94.  LastStopBitsID = UpdateTheChecks(LastStopBitsID,StopBitsMsgID[StopBits]);
  95. }
  96.  
  97. void SetWordLength(int WordLength)
  98. {
  99.  SetPSL(Config.Parity,Config.StopBits,WordLength);
  100.  LastWordLengthID = UpdateTheChecks(LastWordLengthID,WordLengthMsgID[WordLength]);
  101. }
  102.  
  103. void SetPSL(int Parity,int StopBits,int WordLength)
  104. {int RetCode;
  105.  Config.Parity = Parity;
  106.  Config.StopBits = StopBits;
  107.  Config.WordLength = WordLength;
  108.  if(OnLineFlag)
  109.    {RetCode = SioParms(Config.Port,Config.Parity,
  110.                        Config.StopBits,Config.WordLength);
  111.     if(RetCode<0) SioError(RetCode,"SioParms");
  112.    }
  113.  SetTitle();
  114.  /* end SetPSL */
  115. }
  116.  
  117. void SetBaud(int BaudRate)
  118. {int RetCode;
  119.  Config.BaudRate = BaudRate;
  120.  if(OnLineFlag)
  121.    {RetCode = SioBaud(Config.Port,Config.BaudRate);
  122.     if(RetCode<0) SioError(RetCode,"SioBaud");
  123.    }
  124.  LastBaudRateID = UpdateTheChecks(LastBaudRateID,BaudRateMsgID[BaudRate]);
  125.  SetTitle();
  126. } /* end SetBaud */
  127.  
  128. void SetPort(int Port)
  129. {
  130.  if(!ExpectOffLine()) return;
  131.  Config.Port = Port;
  132.  LastPortID = UpdateTheChecks(LastPortID,PortMsgID[Port]);
  133.  SetTitle();
  134. } /* end SetPort */
  135.  
  136. int GetBaud(void)
  137. {return (Config.BaudRate);
  138. }
  139.  
  140. int GetParity(void)
  141. {return (Config.Parity);
  142. }
  143.  
  144. int GetStopBits(void)
  145. {return (Config.StopBits);
  146. }
  147.  
  148. int GetWordLength(void)
  149. {return(Config.WordLength);
  150. }
  151.  
  152. int GetPort(void)
  153. {return (Config.Port);
  154. }
  155.  
  156. void SetTitle()
  157. {char Text[80];
  158.  sprintf(Text,"SIMPLE: COM%d @ %ld baud %c%c%c %sLINE",
  159.     Config.Port+1,
  160.     BaudRateList[Config.BaudRate],
  161.     WordLengthList[Config.WordLength],
  162.     ParityList[Config.Parity],
  163.     StopBitList[Config.StopBits],
  164.     LineText[OnLineFlag&1]);
  165.  SetWindowText(hMainWnd,Text);
  166. } /* end SetTitle */
  167.  
  168. void CheckTheMenu(int MenuID)
  169. {HMENU hMenu;
  170.  if(MenuID)
  171.    {hMenu = GetMenu(hMainWnd);
  172.     CheckMenuItem(hMenu,MenuID,MF_BYCOMMAND | MF_CHECKED);
  173.    }
  174. } /* end CheckTheMenu */
  175.  
  176. void UncheckTheMenu(int MenuID)
  177. {HMENU hMenu;
  178.  if(MenuID)
  179.    {hMenu = GetMenu(hMainWnd);
  180.     CheckMenuItem(hMenu,MenuID,MF_BYCOMMAND | MF_UNCHECKED);
  181.    }
  182. } /* end CheckTheMenu */
  183.  
  184. void UncheckAll(void)
  185. {UncheckTheMenu(LastPortID);
  186.  UncheckTheMenu(LastParityID);
  187.  UncheckTheMenu(LastStopBitsID);
  188.  UncheckTheMenu(LastWordLengthID);
  189.  UncheckTheMenu(LastBaudRateID);
  190. } /* end UncheckAll */
  191.  
  192. void CheckAll(void)
  193. {CheckTheMenu(LastPortID);
  194.  CheckTheMenu(LastParityID);
  195.  CheckTheMenu(LastStopBitsID);
  196.  CheckTheMenu(LastWordLengthID);
  197.  CheckTheMenu(LastBaudRateID);
  198. } /* end CheckAll */
  199.  
  200. int UpdateTheChecks(int OldMsgID,int NewMsgID)
  201. {UncheckTheMenu(OldMsgID);
  202.  CheckTheMenu(NewMsgID);
  203.  return NewMsgID;
  204. } /* end UpdateTheChecks */
  205.